package com.module.product;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import base.util.StringUtil;

import com.module.product.manager.AttrGroupManager;
import com.module.product.manager.CategoryManager;
import com.module.product.vo.Category;
import com.module.sys.LoginSession;

public class CategoryService {

	private CategoryManager categoryManager = CategoryManager.getInstance();
	private AttrGroupManager attrGroupManager=AttrGroupManager.getInstance();
	 public List list()
	 {		 
		 try {
			 Category obj =new Category("0");
			 obj.setOrgId(LoginSession.getOrgid());
			 List<Category> categoryList = categoryManager.getCategoryTree(obj);
			 List list=attrGroupManager.getCateAttrGroupList();
			 for(Category cg:categoryList){
				 for(Object object:list){
					if(object!=null){
						Object[] objs = (Object[])object; 
						System.out.println("======================"+cg.getId().equals((String)objs[0]));
						if(cg.getId().equals((String)objs[0])){					
							
							cg.setAttrGroupNum((Long)objs[1]);							
						}
					}
				 }
			 }
			 return categoryList;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	 }
	 
	 
	
	 public Category findbyid(String id)
	 {
	    try { 
	    		Category category=categoryManager.get(id);
	    		return category;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	 }
	 
	 
	 
	
	public Map save(Category obj) {
		Map<String, Object> statusMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isBlank(obj.getId()))// 添加
			{
				obj.setCreaterId(LoginSession.getUserid());
			} else// 修改
			{
				obj.setUpdaterId(LoginSession.getUserid());
			}
			boolean flag = categoryManager.add(obj);
			statusMap.put("state", flag);
			if (flag == true) {
				statusMap.put("msg", "保存成功");
			} else {
				statusMap.put("msg", "保存失败");
			}

		} catch (Exception e) {
			e.printStackTrace();

		}
		return statusMap;
	}
	 
	 
	 public Map remove(List<String> idArray) throws Exception{
		 String ids = StringUtil.getNewStrByList(idArray);
		 Category obj = new Category();
		 obj.setId(ids);
		 obj.setUpdaterId(LoginSession.getUserid());
		 boolean flag=categoryManager.checkProducByCategory(obj.getId());
		 Map<String, Object> statusMap = new HashMap<String, Object>();
		 if(flag){
			 //不能删除
		 }else{
			 boolean isTrue=categoryManager.delete(obj);
			 statusMap.put("state", isTrue);
			 if (isTrue == true) {
					statusMap.put("msg", "删除成功");
				} else {
					statusMap.put("msg", "删除失败");
				}
		 }
		 return statusMap;
	 }
	 
}
